package top.bug.blog.controller.backend;

import cn.hutool.core.util.StrUtil;
import com.upyun.UpException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.bug.blog.annotation.Auth;
import top.bug.blog.common.Result;
import top.bug.blog.enums.ErrorCode;
import top.bug.blog.exception.BussinessException;
import top.bug.blog.utils.UpYunUtil;

import java.io.IOException;

/**
 * @author <a href="https://github.com/jiusi66">九思.</a>
 * @email 10837907@qq.com
 * @from <a href="https://www.52bug.top">递归茶馆-www.52bug.top</a>
 * @description 后台-公共接口模块
 */
@Api(tags = "公共接口")
@RestController
@RequestMapping("/commonApi")
@Auth
public class CommonController {

    @PostMapping("/upload")
    @ApiOperation(value = "文件上传", notes = "返回上传后的url")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file,
                                     @RequestParam("filePath") String filePath) throws UpException, IOException {
        if (null == file || StrUtil.isEmpty(filePath)) {
            throw new BussinessException(ErrorCode.PARAM_NOT_COMPLETE);
        }
        String fileUrl = UpYunUtil.upload(file, filePath);
        return Result.success(fileUrl);
    }
}